package com.lcx.plugin.neteaseCloud.component.toolwindow.login;

import com.intellij.openapi.ui.DialogWrapper;
import com.lcx.plugin.neteaseCloud.music.DataCenter;
import com.lcx.plugin.neteaseCloud.music.UserInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author wengyongcheng
 * @since 2020/3/2 10:36 下午
 */
public class LogoutDialogWrapper extends DialogWrapper {

    private JLabel logoutLabel;

    private CustomLogoutAction okAction;
    private DialogWrapperExitAction exitAction;

    public LogoutDialogWrapper() {
        super(true);
        init();
        setTitle("登出 (1/1)");
    }


    /**
     * 创建视图
     *
     * @return
     */
    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        logoutLabel = new JLabel("确定登出吗？");
        panel.add(logoutLabel, BorderLayout.NORTH);
        return panel;

    }

    /**
     * 覆盖默认的ok/cancel按钮
     *
     * @return
     */
    @NotNull
    @Override
    protected Action[] createActions() {
        exitAction = new DialogWrapperExitAction("取消", CANCEL_EXIT_CODE);
        okAction = new CustomLogoutAction();
        // 设置默认的焦点按钮
        okAction.putValue(DialogWrapper.DEFAULT_ACTION, true);
        return new Action[]{exitAction, okAction};
    }

    /**
     * 自定义 ok Action
     */
    protected class CustomLogoutAction extends DialogWrapperAction {

        protected CustomLogoutAction() {
            super("确定");
        }

        @Override
        protected void doAction(ActionEvent e) {
            UserInfo.logout();
            close(CANCEL_EXIT_CODE);
        }
    }
}
